﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Org.BouncyCastle.Asn1.X509;
using WallpaperDemo.Models;

namespace WallpaperDemo.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UsersController : ControllerBase
    {
        private readonly WallpapersContext _context;
        private readonly UserService userService;

        public UsersController(WallpapersContext context)
        {
            _context = context;
            userService = new UserService(context);
        }

        // GET: api/Users
        [HttpGet]
        public ActionResult<IEnumerable<User>> GetUsers()
        {
          if (_context.Users == null)
          {
              return NotFound();
          }
            return userService.Users;
        }

        // GET: api/Users/5
        [HttpGet("{id}")]
        public ActionResult<User> GetUser(string id)
        {
            if (_context.Users == null)
            {
                return NotFound();
            }
            var user = userService.QueryUser_Id(id);

            if (user == null)
            {
                return NotFound();
            }

            return user;
        }

        // GET: api/Users/admin
        [HttpGet("{name}")]
        public ActionResult<User> GetUser_LogIn(string name)
        {
            if (_context.Users == null)
            {
                return NotFound();
            }
            var user = userService.QueryUser_Name(name);

            if (user == null)
            {
                return NotFound();
            }

            return user;
        }

        // GET: api/Users/login
        [HttpGet("login")]
        public ActionResult<User> GetUser_LogIn(string name, string password)
        {
            if (_context.Users == null)
            {
                return NotFound();
            }
            var user = userService.QueryUser_LogIn(name, password);

            if (user == null)
            {
                return NotFound();
            }

            return user;
        }

        // GET: api/Users/1/ball
        [HttpGet("{userid}/{tag}")]
        public ActionResult<List<Like>> GetUser_Tag(string userid, string tag)
        {
            var likes = userService.QueryUser_Tag(userid, tag);
            if (likes == null)
            {
                return NotFound();
            }

            return likes;
        }

        // PUT: api/Users/5
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPut("{id}")]
        public IActionResult PutUser(string id, User user)
        {
            if (id != user.UserId)
            {
                return BadRequest();
            }

            try
            {
                userService.RemoveUser(id);
                userService.AddUser(user);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/Users
        [HttpPost]
        public ActionResult<User> AddUser(User user)
        {
            try
            {
                user.IsLogin = false;
                userService.AddUser(user);
            }
            catch (Exception e)
            {
                return BadRequest(e.InnerException.Message);
            }

            return user;
        }

        // POST: api/Users/Likes
        [HttpPost("Likes")]
        public ActionResult<Like> AddLike(Like like)
        {
            try
            {
                like.LikeId = Guid.NewGuid().ToString();
                userService.AddLike(like);
            }
            catch (Exception e)
            {
                return BadRequest(e.InnerException.Message);
            }

            return like;
        }

        // DELETE: api/Users/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteUser(string id)
        {
            if (_context.Users == null)
            {
                return NotFound();
            }
            var user = await _context.Users.FindAsync(id);
            if (user == null)
            {
                return NotFound();
            }

            _context.Users.Remove(user);
            await _context.SaveChangesAsync();

            return NoContent();
        }

        private bool UserExists(string id)
        {
            return (_context.Users?.Any(e => e.UserId == id)).GetValueOrDefault();
        }
    }
}
